/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javagameengine;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

/**
 *
 * Main Class
 *
 * @author jimi
 */
public class JavaGameEngine
{

	static long current_tick = 0;

	public static long getTicks( long time )
	{
		long time_passed = System.currentTimeMillis() - time;
		return time_passed;
	}
	public static double desired_fps = 60;
	static RenderScreen rs = null;

	/**
	 * @param args the command line arguments
	 */
	public static void main( String[] args ) throws InterruptedException
	{
		// TODO code application logic here
		current_tick = System.currentTimeMillis();


		rs = new RenderScreen( new Rectangle( 0, 0, RenderScreen.ratio_x, RenderScreen.ratio_y ), "window" );

		boolean run = true;

		Shape s = new Shape();
		s.color = Color.WHITE;

		s.verts.add( new Point( 000, 000, 000 ) );
		s.verts.add( new Point( 500, 000, 000 ) );
		s.verts.add( new Point( 500, 500, 000 ) );
		s.verts.add( new Point( 000, 500, 000 ) );


		rs.add_shape( s );
		
		s.verts.add( new Point( 000, 000, 000 ) );
		s.verts.add( new Point( 500, 000, 000 ) );
		Still drawme = new Still() ;
		drawme.load( "/lib/test.png" ) ;
		rs.add_anim( drawme ) ;
		
		//	Matrix.Identity.print_matrix() ;
		//	Matrix.Identity.times( Matrix.Identity ).print_matrix() ;

		rs.camera.y = 0;
		rs.camera.x = 0 ;

		Thread.sleep( 500 );

		while( run )
		{
			double precise_frames = 1000 / desired_fps;
			if( getTicks( current_tick ) < precise_frames )
			{
				Thread.sleep( (long)precise_frames - getTicks( current_tick ) );
			}


			if( Keyboard.is.pressed( KeyEvent.VK_UP ) )
			{
				rs.camera.y++;
			}
			if( Keyboard.is.pressed( KeyEvent.VK_DOWN ) )
			{
				rs.camera.y--;
			}

			if( Keyboard.is.pressed( KeyEvent.VK_RIGHT ) )
			{
				rs.camera.x++;
			}
			if( Keyboard.is.pressed( KeyEvent.VK_LEFT ) )
			{
				rs.camera.x--;
			}

			if( Keyboard.is.pressed( KeyEvent.VK_Z ) )
			{
				((Point)s.verts.get( 0 )).y += 1 ;
			}

			if( Keyboard.is.pressed( KeyEvent.VK_X ) )
			{
				((Point)s.verts.get( 0 )).y -= 1 ;
			}
			{
				rs.push_matrix( rs.camera_translation() );
				rs.paint();
				rs.pop_matrix();
				
				current_tick = System.currentTimeMillis();

				Keyboard.is.OnKeyTriggered = new function()
				{
					@Override
					public void execute() /* this feels stupid... */
					{
					}

					@Override
					public void execute( int kc )
					{
						if( kc == KeyEvent.VK_BACK_SLASH )
						{
							rs.DisplayFPS = !rs.DisplayFPS;
						}
					}
				};
			}

			if( Keyboard.is.pressed( KeyEvent.VK_CONTROL ) && Keyboard.is.pressed( KeyEvent.VK_Q ) )
			{
				run = false;
				rs.frame.dispose();
			}


		}

	}
}
